package ru.compft.web.adapter;

import org.springframework.util.CollectionUtils;
import ru.compft.common.data.AppRole;
import ru.compft.common.data.AppUser;
import ru.compft.web.beans.RoleBean;
import ru.compft.web.beans.UserBean;

import java.util.List;
import java.util.Set;

/**
 * User: ASUS
 * Date: 09.08.12
 * Time: 21:09
 * Адаптер для преобразование бина в объект БД
 */
public class UserAdapter {
    /**
     * Преобразование web-бина в объект БД
     *
     * @param userBean
     * @return
     */
    public static AppUser convertToDataObject(UserBean userBean) {
        if (userBean == null) return null;

        final AppUser appUser = new AppUser();

        appUser.setId(userBean.getId());
        appUser.setLogin(userBean.getLogin());
        appUser.setFirstName(userBean.getFirstName());
        appUser.setLastName(userBean.getLastName());
        appUser.setEnabled(userBean.isEnabled());
        appUser.setAccountNonExpired(userBean.isAccountNonExpired());
        appUser.setCredentialsNonExpired(userBean.isCredentialsNonExpired());
        appUser.setAccountNonLocked(userBean.isAccountNonLocked());
        appUser.setRegDate(userBean.getRegDate());
        appUser.setLastActivity(userBean.getLastActivity());

        // конвертируем роли
        final List<RoleBean> roleBeanList = userBean.getRoles();
        if (!CollectionUtils.isEmpty(roleBeanList)) {
            for (RoleBean roleBean : roleBeanList) {
                final AppRole appRole = RoleAdapter.convertToDataObject(roleBean);
                if (appRole != null) appUser.addRole(appRole);
            }
        }

        return appUser;
    }

    /**
     * Копирование объекта БД в web-бин
     *
     * @param appUser
     * @param userBean
     */
    public static void copyToBean(AppUser appUser, UserBean userBean) {
        if (userBean != null && appUser != null) {

            userBean.setId(appUser.getId());
            userBean.setLogin(appUser.getLogin());
            userBean.setFirstName(appUser.getFirstName());
            userBean.setLastName(appUser.getLastName());
            userBean.setEnabled(appUser.isEnabled());
            userBean.setAccountNonExpired(appUser.isAccountNonExpired());
            userBean.setCredentialsNonExpired(appUser.isCredentialsNonExpired());
            userBean.setAccountNonLocked(appUser.isAccountNonLocked());
            userBean.setRegDate(appUser.getRegDate());
            userBean.setLastActivity(appUser.getLastActivity());

            // конвертируем роли
            final Set<AppRole> appRoleList = appUser.getRoles();
            if (!CollectionUtils.isEmpty(appRoleList)) {
                for (AppRole appRole : appRoleList) {
                    final RoleBean roleBean = new RoleBean();
                    RoleAdapter.copyToBean(appRole, roleBean);
                    userBean.addRole(roleBean);
                }
            }
        }
    }
}
